package cn.lanchengkeji.datafillingsystem.service.impl;



import cn.lanchengkeji.datafillingsystem.entity.Dictionary;
import cn.lanchengkeji.datafillingsystem.mapper.DictionaryMapper;
import cn.lanchengkeji.datafillingsystem.service.IDictionaryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 字典表 服务实现类
 * </p>
 *
 * @author Yixu
 * @since 2018-12-06
 */
@Service
public class DictionaryServiceImpl extends ServiceImpl<DictionaryMapper, Dictionary> implements IDictionaryService {

    public List<Dictionary> getListByGroup(String group){
        Dictionary condition = new Dictionary();
        condition.setDictGroup(group);
        List<Dictionary> list = getByCondition(condition);
        return list;
    }
    public List<Dictionary> getListByGroupAndParterns(String group, String patterns1, String patterns2, String patterns3){
        Dictionary condition = new Dictionary();
        condition.setDictGroup(group);
        condition.setPattern1(patterns1);
        condition.setPattern2(patterns2);
        condition.setPattern3(patterns3);
        List<Dictionary> list = getByCondition(condition);
        return list;
    }
    public Dictionary getValueByGroupAndCode(String group, String code){
        Dictionary condition = new Dictionary();
        condition.setDictGroup(group);
        condition.setCode(code);
        QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(condition);
        Dictionary dictionary = this.getOne(queryWrapper);
        return dictionary;
    }

    @Override
    public void evaluate(List<?> list, String[] group, String sourceField, String targetField) {
        if (list == null || list.isEmpty()) {
            return;
        }
        if (group == null || sourceField == null || targetField == null) {
            return;
        }
        String[] sourceFields = sourceField.split(",");
        String[] targetFields = targetField.split(",");
        if (group.length != sourceFields.length || sourceFields.length != sourceFields.length) {
            return;
        }
        Dictionary condition = new Dictionary();
        for (int i = 0; i < sourceFields.length; i++) {
            condition.setDictGroup(group[i]);
            QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<>();
            queryWrapper.setEntity(condition);
            List<Dictionary> dictionaryList = this.list(queryWrapper);
            Map<String, String> codeMap = new HashMap();
            for (Dictionary dictionary : dictionaryList) {
                codeMap.put(dictionary.getCode(), dictionary.getDictValue());
            }
            for (Object obj : list) {
                try {
                    String code = BeanUtils.getProperty(obj, sourceFields[i]);
                    String value = codeMap.get(code);
                    BeanUtils.setProperty(obj, targetFields[i], value);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private List<Dictionary> getByCondition(Dictionary condition){
        QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(condition).orderByAsc(Dictionary.SEQ);
        List<Dictionary> list = this.list(queryWrapper);
        return list;
    }
}
